package com.project.geonotes;

import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class TouchOverlay extends Overlay {

	int lastX = 0;
	int lastY = 0;
	double percentage = .05;
	int [] lastPoint = new int[]{0,0};
	MapFrag superMapFrag = null;
	private DBAdapter db;
	
	public TouchOverlay(MapFrag theFrag, DBAdapter db) {
		super();
		this.db = db;
		superMapFrag = theFrag;
		// TODO Auto-generated constructor stub
	}
	
//	private boolean getLocationOfCoord(int[] newPoint,int[] lastPoint MapView mapView){
//		mapView.getLatitudeSpan();
//		mapView.getLongitudeSpan();
//		if (lastPoint[0]-radius < newPoint[0] && lastPoint[0]+radius>newPoint[0] &&
//				lastPoint[1]-radius < newPoint[1] && lastPoint[1]+radius>newPoint[1])
//	}
	
	@Override
	public boolean onTap(GeoPoint point, MapView mapView){
		
		
		int [] newPoint = new int[] {point.getLatitudeE6(),point.getLongitudeE6()};
		
		//mapView.getLocationOnScreen(newPoint);
		
		Log.e("newPointx", String.valueOf(newPoint[0]));
		Log.e("newPointy", String.valueOf(newPoint[1]));
		Log.e("lastPointx", String.valueOf(lastPoint[0]));
		Log.e("lastPointy", String.valueOf(lastPoint[1]));
		
		Double radiusLat = percentage*mapView.getLatitudeSpan();
		Double radiusLong = percentage*mapView.getLongitudeSpan();
		
		if (lastPoint[0]-radiusLat < newPoint[0] && lastPoint[0]+radiusLat>newPoint[0] &&
				lastPoint[1]-radiusLong < newPoint[1] && lastPoint[1]+radiusLong>newPoint[1]){
			lastPoint = new int[] {0,0};
			Note test = new Note(-1, newPoint[0],newPoint[1],"","","", db, superMapFrag);
			test.createDialog(superMapFrag.getActivity(), false);
//			Log.e("Got somewhere", ":/");
//			List<Overlay> mapOverlays = mapView.getOverlays();
//	        Drawable drawable = mapView.getResources().getDrawable(R.drawable.androidmarker);
//	        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, mapView.getContext());
//	        //mapView.getR
//	        
//	        OverlayItem overlayitem = new OverlayItem(point, "Bonjour", "Random Point");
//	        itemizedoverlay.addOverlay(overlayitem);
//	        mapOverlays.add(itemizedoverlay);
			return true;
		}
		
		else{
			lastPoint = newPoint;
			return false;
		}
	}
	
//	@Override
//	public boolean onTouchEvent(MotionEvent e, MapView mapView){
//		
//		
//		if (lastX-radius < e.getX() && lastX+radius>e.getX() &&
//				lastY-radius < e.getY() && lastY+radius>e.getY()){
//			
//			lastX = (int) 0;
//			lastY = (int) 0;
//			
//			
//			Log.e("Got somewhere", ":/");
//			List<Overlay> mapOverlays = mapView.getOverlays();
//	        Drawable drawable = mapView.getResources().getDrawable(R.drawable.androidmarker);
//	        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, mapView.getContext());
//	        //mapView.getR
//	        GeoPoint point = mapView.getProjection().fromPixels(
//	        		(int) e.getX(), (int) e.getY());
//	        
//	        OverlayItem overlayitem = new OverlayItem(point, "Bonjour", "Random Point");
//	        itemizedoverlay.addOverlay(overlayitem);
//	        mapOverlays.add(itemizedoverlay);
//			return true;
//		}
//		
//		else{
//			lastX = (int) e.getX();
//			lastY = (int) e.getY();
//			return false;
//		}
//	}

}
